package com.weu.contorller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.weu.bean.*;
import com.weu.service.IBrandService;
import com.weu.service.ICarTypeService;
import com.weu.service.IModelService;
import com.weu.service.ISeriesService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class CarModelController {

    @Resource
    private IModelService iModelService;

    @Resource
    private ISeriesService iSeriesService;

    @Resource
    private IBrandService iBrandService;

    @Resource
    private ICarTypeService iCarTypeService;


    @ResponseBody
    @RequestMapping("/carModels")
    public Result getCarModel(@RequestBody JSONObject jsonParam) {
        int seriesId = jsonParam.getIntValue("seriesId");
        System.out.println("解析JSON格式数据seriesId：" + seriesId);
        return iModelService.getBySeriesId(seriesId);
    }

    /**
     * 分页显示汽车类型
     *
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @GetMapping("/carModels")
    public String getAllCarModels(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                  Model model) {
        //为了程序的严谨性，判断非空：
        if (pageNum == null) {
            pageNum = 1;   //设置默认当前页
        }
        if (pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;    //设置默认每页显示的数据数
        }
        System.out.println("当前页是：" + pageNum + "显示条数是：" + pageSize);

        PageHelper.startPage(pageNum, pageSize);
        try {
            List<CarModel> list = iModelService.getAll();
            System.out.println("分页数据：" + list);
            PageInfo<CarModel> pageInfo = new PageInfo<CarModel>(list, pageSize);
            model.addAttribute("pageInfo", pageInfo);
        } finally {
            PageHelper.clearPage();
        }
//  获取所有系列
        List<Series> seriesList = iSeriesService.getAll();
        model.addAttribute("seriesList", seriesList);

        //5.设置返回的jsp/html等前端页面
        return "/backstage/carModel/list";
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/carModel/{id}")
    public String deleteSeries(@PathVariable("id") int id) {
        iModelService.delById(id);
        return "redirect:/carModels";
    }


    /**
     * 来到修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/carModel/{id}")
    public String toEditPage(@PathVariable(value = "id") int id, Model model) {
        CarModel carModel = iModelService.getOne(id);
        model.addAttribute("carModel", carModel);
        //  获取所有系列
        List<Series> seriesList = iSeriesService.getAll();
        model.addAttribute("seriesList", seriesList);
        //  将数据回显到编辑页面
        return "backstage/carModel/edit";
    }

    /**
     * 修改
     *
     * @param carModel
     * @return
     */
    @PutMapping("/carModel")
    public String editCarModel(CarModel carModel) {
        //  修改后的数据
        System.out.println("修改后的数据" + carModel);
        iModelService.updateModel(carModel);
        //  重定向
        return "redirect:/carModels";
    }

    /**
     * 来到添加页面
     *
     * @param model
     * @return
     */
    @GetMapping("/carModel")
    public String toSavePage(Model model) {
        //  获取所有系列
        List<Series> seriesList = iSeriesService.getAll();
        model.addAttribute("seriesList", seriesList);

        //  获取所有品牌
        List<Brand> brandList = iBrandService.getAll();
        model.addAttribute("brandList", brandList);

        //  获取所有类型
        List<CarType> carTypeList = iCarTypeService.getAll();
        model.addAttribute("carTypeList", carTypeList);

        return "backstage/carModel/save";

    }

    /**
     * 添加
     *
     * @param carModel
     * @return
     */
    @PostMapping("/carModel")
    public String saveCarModel(CarModel carModel) {
        System.out.println("保存的汽车型号：" + carModel);
        iModelService.saveModel(carModel);
        //  添加成功，重定向到查询页面
        return "redirect:/carModels";
    }
}
